#include "com_esun_libc_ESInterface.h"
#include "touzhu.h"

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>

#include <android/log.h>
#define TAG "jni"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)


/*
 * Class:     com_esun_cxyz_ESInterface
 * Method:    getZYGGMaxMoney
 * Signature: ([Ljava/lang/Object;[III)F
 */
JNIEXPORT jdouble JNICALL Java_com_esun_libc_ESInterface_getZYGGMaxMoney ( JNIEnv* env , jclass obj , jobjectArray touZhuData_ , jintArray array, jint beiShu, jint changShu,jint lottery_type){
	 jint i,j;

    const int size = (*env)->GetArrayLength(env, touZhuData_);//获得行数
    TouzhuData dataArray[size];
    jclass objectClass = (*env) -> FindClass(env , "com/esun/libc/TouZhuData");
    for(i = 0 ; i < size ; i++)
    {
        jobject objTmp = (*env)->GetObjectArrayElement(env, touZhuData_, i );



        // 胜赔率
        jfieldID winPL = (*env) -> GetFieldID(env ,objectClass,"winPL","F");
        // 平赔率
        jfieldID drawPL = (*env) -> GetFieldID(env ,objectClass,"drawPL","F");
        // 负赔率
        jfieldID losePL = (*env) -> GetFieldID(env ,objectClass,"losePL","F");
        // 是否为胆码
        jfieldID isDan = (*env) -> GetFieldID(env ,objectClass,"isDan","I");
        // 是否为有效场次
        jfieldID isEff = (*env) -> GetFieldID(env ,objectClass,"isEff","I");


        dataArray[i].win = (*env) -> GetFloatField(env , objTmp , winPL);
        dataArray[i].draw = (*env) -> GetFloatField(env , objTmp , drawPL);
        dataArray[i].lose = (*env) -> GetFloatField(env , objTmp , losePL);
        dataArray[i].is_dan = (*env) -> GetIntField(env , objTmp , isDan);
        dataArray[i].is_eff = (*env) -> GetIntField(env , objTmp , isEff);

        (*env)->DeleteLocalRef(env,objTmp);

    }


    jint* pArray = (*env) -> GetIntArrayElements(env ,array , NULL);
    jsize len = (*env) -> GetArrayLength(env ,array);

    (*env)->DeleteLocalRef(env,objectClass);

//    int index = 0;
//    for(index = 0; index < changShu; index++)
//    {
//    	LOGD("win=%f,draw=%f,lose=%f",dataArray[index].win,dataArray[index].draw,dataArray[index].lose);
//    }
//    for(index = 0; index<len; index++)
//    {
//    	LOGD("pArray[%d] = %d",index,pArray[index]);
//
//    }

    double result = (jdouble)get_ziyou_max_money(dataArray ,changShu, pArray ,len, beiShu , lottery_type);
    return result;
}

/*
 * Class:     com_esun_cxyz_ESInterface
 * Method:    getZYTouZhuNum
 * Signature: (I[[I[I)I
 */
JNIEXPORT jint JNICALL Java_com_esun_libc_ESInterface_getZYTouZhuNum ( JNIEnv* env , jclass obj , jint gameNum , jobjectArray pDetail, jintArray pGGType, jint lottery_type) {
	 //LOGI(" _________ getZYTouZhuNum [STEP  =1= ] _________________");

    jint* pggValue = (*env) -> GetIntArrayElements(env ,pGGType , NULL);
    int pggLen = (*env) -> GetArrayLength(env ,pGGType);

    jint i,j;

    int size = (*env)->GetArrayLength(env, pDetail);//获得行数

    jarray myarray = ((*env)->GetObjectArrayElement(env, pDetail, 0));

    int col = (*env)->GetArrayLength(env, myarray); //获得列数

    (*env)->DeleteLocalRef(env,myarray);

    jint qizi[size][col];

    for (i = 0; i < size; i++)

    {

        myarray = ((*env)->GetObjectArrayElement(env, pDetail, i));

        jint *coldata = (*env)->GetIntArrayElements(env, (jintArray)myarray, NULL );

        for (j=0; j<col; j++)
        {
           qizi [i][j] = coldata[j];
            //LOGI(" _________(%d)(%d) getZYTouZhuNum [STEP  =2.1= ]{i get Num is : %d} _________________" , i , j , qizi[i][j]);
        }

        (*env)->ReleaseIntArrayElements(env, myarray, coldata, 0);
        (*env)->DeleteLocalRef(env,myarray);
    }

    //LOGI(" _________ getZYTouZhuNum [STEP  =3= ] _________________");
    jint result = (jint)get_ziyou_stake_count(qizi  , gameNum,  pggValue, pggLen,lottery_type);
    (*env)->ReleaseIntArrayElements(env, pGGType, pggValue, 0);
    return result;
}


/*
 * Class:     com_esun_cxyz_ESInterface
 * Method:    getDCTouZhuNum
 * Signature: (I[[I[I)I
 */
JNIEXPORT jint JNICALL Java_com_esun_libc_ESInterface_getDCTouZhuNum ( JNIEnv* env , jclass obj , jint gameNum , jobjectArray pDetail , jintArray pGGType,jint lottery_type) {

	 jint i,j;

    int size = (*env)->GetArrayLength(env, pDetail);//获得行数

    jarray myarray = ((*env)->GetObjectArrayElement(env, pDetail, 0));

    int col = (*env)->GetArrayLength(env, myarray); //获得列数

    jint qizi[size][col];
    (*env)->DeleteLocalRef(env,myarray);

    for (i = 0; i < size; i++)
    {
        myarray = ((*env)->GetObjectArrayElement(env, pDetail, i));

        jint *coldata = (*env)->GetIntArrayElements(env, (jintArray)myarray, 0 );

        for (j=0; j<col; j++)
        {
           qizi [i][j] = coldata[j];
        }
        (*env)->ReleaseIntArrayElements(env, myarray, coldata, 0);
        (*env)->DeleteLocalRef(env,myarray);
    }

    jint* pValue = (*env) -> GetIntArrayElements(env ,pGGType , NULL);
    int result = (jint)get_duochuan_stake_count(qizi ,gameNum, pValue,lottery_type);
    (*env)->ReleaseIntArrayElements(env, pGGType, pValue, 0);
    return result;
}

/*
 * Class:     com_esun_cxyz_ESInterface
 * Method:    getDCGGMaxMoney
 * Signature: ([Ljava/lang/Object;[III)F
 */
JNIEXPORT jdouble JNICALL Java_com_esun_libc_ESInterface_getDCGGMaxMoney (JNIEnv* env , jclass obj , jobject touZhuData_ , jintArray orgGGTypeArr , jint beiShu , jint allGameNum,jint lottery_type) {
	 const int size = (*env)->GetArrayLength(env, touZhuData_);
	 TouzhuData dataArray[size];

    int k=0;

    jclass objectClass = (*env) -> FindClass(env , "com/esun/libc/TouZhuData");
    for(k=0;k<size;k++)
    {
        jobject objTmp = (*env)->GetObjectArrayElement(env, touZhuData_, k);

        //struct touZhuData* data = (struct touZhuData*)malloc(sizeof(struct touZhuData));



        // 胜赔率
        jfieldID winPL = (*env) -> GetFieldID(env ,objectClass,"winPL","F");

        // 平赔率
        jfieldID drawPL = (*env) -> GetFieldID(env ,objectClass,"drawPL","F");

        // 负赔率
        jfieldID losePL = (*env) -> GetFieldID(env ,objectClass,"losePL","F");

        // 是否为胆码
        jfieldID isDan = (*env) -> GetFieldID(env ,objectClass,"isDan","I");

        // 是否为有效场次
        jfieldID isEff = (*env) -> GetFieldID(env ,objectClass,"isEff","I");

        dataArray[k].win = (*env) -> GetFloatField(env , objTmp , winPL);
        dataArray[k].draw = (*env) -> GetFloatField(env , objTmp , drawPL);
        dataArray[k].lose = (*env) -> GetFloatField(env , objTmp , losePL);
        dataArray[k].is_dan = (*env) -> GetIntField(env , objTmp , isDan);
        dataArray[k].is_eff = (*env) -> GetIntField(env , objTmp , isEff);

        (*env)->DeleteLocalRef(env,objTmp);

        //dataArray[k] = data;
    }


//    int index = 0;
//    for(index = 0; index < allGameNum; index++)
//    {
//    	LOGD("win=%f,draw=%f,lose=%f",dataArray[index].win,dataArray[index].draw,dataArray[index].lose);
//    }

    jint* pValue = (*env) -> GetIntArrayElements(env ,orgGGTypeArr , NULL);
    double result = (jdouble)get_duochuan_max_money(dataArray , pValue , beiShu , allGameNum,lottery_type);
    (*env)->ReleaseIntArrayElements(env, orgGGTypeArr, pValue, 0);
    (*env)->DeleteLocalRef(env,objectClass);
    return result;
}
